package TeachLesson;

import TeachLesson.Res.ResDao;
import TeachLesson.Res.ResEnity;
import com.jspsmart.upload.SmartUpload;
import com.jspsmart.upload.SmartUploadException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

@WebServlet(name = "DleteResServlet")
public class downloadResServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
           doGet(request,response);

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

       // response.setCharacterEncoding("GBK");
        //request.setCharacterEncoding("GBK");

        int id=Integer.parseInt(request.getParameter("id"));
        ResDao resDao=new ResDao();
        ResEnity resEnity=resDao.getRes(id);
        String filePath=resEnity.getFileRoot()+"/"+resEnity.getFileName();

        /*File file=new File(filePath);
        if(file.exists()){
            file.delete();
        }
        response.sendRedirect("");*/

        SmartUpload su=new SmartUpload();
        //初始化
        su.initialize(getServletConfig(),request,response);
        //下载
        try {
            su.downloadFile(filePath);
        } catch (SmartUploadException e) {
            e.printStackTrace();
        }
    }

}
